from socket import *
from tkinter import *
from random import *
from turtle import *
import time
from queue import Queue
try:
    import _thread
except:
    import thread
from os import *
host = ""
name = 0
me = socket(AF_INET,SOCK_STREAM)
port = 12345
#菜单
def click():
    global host,me,port
    host = hst.get()
    #网络连接
    me.connect((host,port))
    welcome = me.recv(1024) #等待游戏开始
    print(welcome.decode("ASCII"))
    window.destroy()
    welcome = me.recv(1024)
    print("Start")
window = Tk()
lab = Label(window,text="Hostname")
hst = Entry(window)
ok = Button(window,text="OK",command=click)
lab.grid(column=1,row=1)
hst.grid(column=2,row=1)
ok.grid(column=3,row=1)

window.mainloop()


###########################################################

if('''未连接成功'''):
    '''结束'''

# init
N=100 #最多操作数
n=10
m=10
color=['grey','red','blue']
screensize(canvwidth=2000, canvheight=2000, bg='black')
turn=0
numofpls=2
delay(0)

register_shape('pictures\\block_red.gif')
register_shape('pictures\\block_blue.gif')
register_shape('pictures\\block_grey.gif')
register_shape('pictures\\mount.gif')
register_shape('pictures\\castle.gif')
register_shape('pictures\\home.gif')
register_shape('pictures\\swamp.gif')
register_shape('pictures\\highlight.gif')
class Block:
    def __init__(self,ty):
        self.type=int(ty) #0地 1山 2塔 3大本营 4沼泽
        self.army=0
        self.color=0
        if self.type==2:
            self.army=choice(range(40,50))
        self.lazytag=True
    def upd(self,t,a,c):
        self.type=t
        self.army=a
        self.color=c
        self.lazytag=True
map=[0]
for i in range(1,n+1):
    map.append([0])
    for j in range(1,m+1):
        map[i].append(0)
opt=Queue(maxsize=N)
# 编码发送
def enc(I,optt):
    fx,fy,tx,ty,num=optt
    msg="O"+chr(I)+chr(fx)+chr(fy)+chr(tx)+chr(ty)+chr(num//100)+chr(num%100)
    return msg
def optsd():
    global mynam
    while opt.empty()==False:
        tempopt=opt.get()
        if optjudg(tempopt):
            me.send(enc(mynam,tempopt))
            break

class Players:
    def __init__(self,nn):
        self.die=False
        self.color=nn
        self.army=1
        self.land=1
    def upd(self,a,l):
        self.army=a
        self.land=l
def init():
    # m,n,color[],numofpls,map[][]
    '''
    '''
    m=10
    n=10
    color=['grey','red','blue']
    numofpls=2
    for i in range(1,n+1):
        for j in range(1,m+1):
            map[i][j]=Block(0)
    '''
    #'''
    player=[0]
    for i in range(1,numofpls+1):
        player.append(Players(i))
    return
def printmap():
    p=Pen()
    p.up()
    p.ht()
    p.color('white')
    p.speed(0)
    tracer(False)
    p.pensize(78)                           ####
    for i in range(1,n+1):
        for j in range(1,m+1):
            if not map[i][j].lazytag: continue
            map[i][j].lazytag=False
            p.goto(80*j-40*m-40,-80*i+40*n+40)               ####
            p.shape('pictures\\block_'+color[map[i][j].color]+'.gif')
            p.stamp()
            if map[i][j].type==1:
                p.shape('pictures\\mount.gif')
            elif map[i][j].type==2:
                p.shape('pictures\\castle.gif')
            elif map[i][j].type==3:
                p.shape('pictures\\home.gif')
            elif map[i][j].type==4:
                p.shape('pictures\\swamp.gif')
            if map[i][j].type!=0:
                p.stamp()
            p.goto(80*j-40*m-40,-80*i+40*n+20)          ####
            if map[i][j].army!=0:
                p.write(map[i][j].army,font=('Arial',30,'normal'),align='center')
    update()
    system('cls')
    for j in range(m+2):
        print("___",end="")
    print("")
    for i in range(1,n+1):
        print("|",end="")
        for j in range(1,m+1):
            if [i,j]==player[1].highlight:
                print("(%3d)"%(map[i][j].army),end="")
            elif [i,j]==player[2].highlight:
                print("[%3d]"%(map[i][j].army),end="")
            else:
                print("%5d"%(map[i][j].army),end="")
            if map[i][j].type==1:
                print("M",end="")
            elif map[i][j].type==2:
                print("C",end="")
            elif map[i][j].type==3:
                print("H",end="")
            elif map[i][j].type==4:
                print("S",end="")
            else: print(" ",end="")
        print("|")
    for j in range(m+2):
        print("___",end="")
    print("")
    print(r1,r2,player[1].highlight,player[2].highlight)

def deco(msg):
    if(msg[0]=="B"):
        x=ord(msg[1])
        y=ord(msg[2])
        map[x][y].army=ord(msg[3])*100+ord(msg[4])
        map[x][y].color=ord(msg[5])
        map[x][y].type=ord(msg[6])#type之前没加 √
        
        #prt(x,y)

    if(msg[0]=="P"):
        pla=msg[1]
        player[pla].army=ord(msg[2])*100+ord(msg[3])
        player[pla].land=ord(msg[4])*100+ord(msg[5])
        player[pla].castle=ord(msg[6])*100+ord(msg[7])#城堡也没加 √
        
        #infoprt(pla)

def ListenServer():
    while True:
        s=me.recv(8)
        deco(s)
        
init()
_thread.start_new_thread(ListenServer,())
# listen player
'''
r=1                                                    #状态：1为调兵，0为非调兵
por=1                                                   #调兵比例：1为100%，0.5为50%,直到0.125为12.5%
class Act:
    def __init__(self,fx,fy,tx,ty,num):
        self.fx=fx
        self.fy=fy
        self.tx=tx
        self.ty=ty
        self.num=num

def wasd(s,t):
    global por
    x=highlight[0]
    y=highlight[1]
    if not (x+s in range(1,n+1) and y+t in range(1,m+1)): return
    if r: 
        opt.put(Act(highlight[0],highlight[1],highlight[0]+s,highlight[1]+t,por),False)
    highlight=[x+s,y+t]
    por=1
    hl.goto(800/min(m,n)*player[1].highlight[1]-400-400/min(m,n),-800/min(m,n)*player[1].highlight[0]+400+400/min(m,n))
    update()
def pw():
    wasd(-1,0)
def pa():
    wasd(0,-1)
def ps():
    wasd(1,0)
def pd():
    wasd(0,1)
def pr():
    global r1
    r1^=1
def pt():
    global por1
    if por1==0.125:
        por1=1
    else: por1=por1/2
onkeypress(pw,'w')
onkeypress(pa,'a')
onkeypress(ps,'s')
onkeypress(pd,'d')
onkeypress(pr,'r')
onkeypress(pt,'t')
onkeypress(optclear1,'q')
listen()
mainloop()
'''
while True : pass






